package com.tutos.ui.monitor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.tutos.model.Evento;
import com.vaadin.addon.calendar.event.CalendarEventEditor;
import com.vaadin.addon.calendar.event.CalendarEvent.EventChangeNotifier;

@SuppressWarnings("serial")
public class Eventos implements CalendarEventEditor, EventChangeNotifier{
	
	private String caption;
	private String description;
	private Date end;
	private Date start;
	private String styleName;
	private Evento ev = new Evento();

	private transient List<EventChangeListener> listeners = new ArrayList<EventChangeListener>();

	private boolean isAllDay;

	/**
	 * Default constructor
	 */
	public Eventos() {
		this.setStyleName("color3");
	}
	
	/**
	 * Default constructor
	 */
	public Eventos(Evento ev) {
		this.ev = ev;
		this.isAllDay = ev.isTodoElDia();
		this.setStyleName("color3");
	}
	
	/**
	 * Constructor for creating an event with the same start and end date
	 * 
	 * @param caption
	 *            The caption for the event
	 * @param description
	 *            The description for the event
	 * @param date
	 *            The date the event occurred
	 */

	public Eventos(String caption, String description, Date date) {
		setCaption(caption);
		setDescription(description);
		ev.setInicio(date);
		ev.setFin(date);
		ev.setTodoElDia(true);
		this.setStyleName("color3");
	}

	/**
	 * Constructor for creating an event with a start date and an end date.
	 * Start date should be before the end date
	 * 
	 * @param caption
	 *            The caption for the event
	 * @param description
	 *            The description for the event
	 * @param startDate
	 *            The start date of the event
	 * @param endDate
	 *            The end date of the event
	 */

	public Eventos(String caption, String description, Date startDate,
			Date endDate) {
		setCaption(caption);
		setDescription(description);
		ev.setInicio(startDate);
		ev.setFin(endDate);
		ev.setTodoElDia(false);
		this.caption = caption;
		this.description = description;
		this.start = startDate;
		this.end = endDate;
		this.setStyleName("color3");
	}

	public Eventos(Date startDate, Date endDate, boolean isAllDay) {
		setCaption(caption);
		setDescription(description);
		ev.setInicio(startDate);
		ev.setFin(endDate);
		ev.setTodoElDia(false);
		this.start = startDate;
		this.end = endDate;
		setAllDay(isAllDay);
		this.setStyleName("color3");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.vaadin.addon.calendar.event.CalendarEvent#getCaption()
	 */
	public String getCaption() {
		return caption;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.vaadin.addon.calendar.event.CalendarEvent#getDescription()
	 */
	public String getDescription() {
		return description;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.vaadin.addon.calendar.event.CalendarEvent#getEnd()
	 */
	public Date getEnd() {
		return ev.getFin();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.vaadin.addon.calendar.event.CalendarEvent#getStart()
	 */
	public Date getStart() {
		return ev.getInicio();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.vaadin.addon.calendar.event.CalendarEvent#getStyleName()
	 */
	public String getStyleName() {
		return styleName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.vaadin.addon.calendar.event.CalendarEvent#isAllDay()
	 */

	public boolean isAllDay() {
		return isAllDay;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.vaadin.addon.calendar.event.CalendarEventEditor#setCaption(java
	 * .lang .String)
	 */
	public void setCaption(String caption) {
		this.caption = caption;
		fireEventChange();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.vaadin.addon.calendar.event.CalendarEventEditor#setDescription
	 * (java .lang.String)
	 */
	public void setDescription(String description) {
		this.description = description;
		fireEventChange();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.vaadin.addon.calendar.event.CalendarEventEditor#setEnd(java.util.
	 * Date)
	 */
	public void setEnd(Date end) {
		this.end = end;
		ev.setFin(end);
		fireEventChange();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.vaadin.addon.calendar.event.CalendarEventEditor#setStart(java
	 * .util .Date)
	 */
	public void setStart(Date start) {
		this.start = start;
		ev.setInicio(start);
		fireEventChange();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.vaadin.addon.calendar.event.CalendarEventEditor#setStyleName(java
	 * .lang.String)
	 */
	public void setStyleName(String styleName) {
		this.styleName = styleName;
		fireEventChange();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.vaadin.addon.calendar.event.CalendarEventEditor#setAllDay(boolean
	 * )
	 */
	public void setAllDay(boolean isAllDay) {
		this.isAllDay = isAllDay;
		ev.setTodoElDia(isAllDay);
		fireEventChange();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.vaadin.addon.calendar.ui.CalendarComponentEvents.EventChangeNotifier
	 * #addListener
	 * (com.vaadin.addon.calendar.ui.CalendarComponentEvents.EventChangeListener
	 * )
	 */
	public void addListener(EventChangeListener listener) {
		listeners.add(listener);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.vaadin.addon.calendar.ui.CalendarComponentEvents.EventChangeNotifier
	 * #removeListener
	 * (com.vaadin.addon.calendar.ui.CalendarComponentEvents.
	 * EventChangeListener )
	 */
	public void removeListener(EventChangeListener listener) {
		listeners.remove(listener);
	}

	/**
	 * Fires an event change event to the listeners. Should be triggered
	 * when some property of the event changes.
	 */
	protected void fireEventChange() {
		EventChange event = new EventChange(this);

		for (EventChangeListener listener : listeners) {
			listener.eventChange(event);
		}
	}

	public Evento getEvento() {
		return ev;
	}

	public void setEvento(Evento ev) {
		this.ev = ev;
		this.setStyleName("color3");
	}
}